package com.han.walkman.timer;

import com.han.walkman.entity.VisitedPlanEntity;
import com.han.walkman.entity.VisitingPlanEntity;
import com.han.walkman.service.VisitedPlanService;
import com.han.walkman.service.VisitingPlanService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: Hanjiafeng
 * @Date: 2020/6/10
 * @Desc:
 */
//@Component
//@EnableScheduling
@Slf4j
public class VisitPlanTimer {
    private final VisitingPlanService visitingPlanService;
    private final VisitedPlanService visitedPlanService;

    public VisitPlanTimer(VisitingPlanService visitingPlanService, VisitedPlanService visitedPlanService) {
        this.visitingPlanService = visitingPlanService;
        this.visitedPlanService = visitedPlanService;
    }

    /**
     * 凌晨过1秒将与拜访表中过时的拜访计划迁入历史拜访计划表中
     */
//    @Scheduled(cron = "01 25 15 * * ?")
    public void sayHello() {
        log.info("开始每日计划清理");
        //查询今天之前的拜访计划
        List<VisitingPlanEntity> visitingPlanEntityList = visitingPlanService.findAllBeforeDate();
        log.info(new Date(System.currentTimeMillis()) + "之前计划共:" + visitingPlanEntityList.size() + ",将其转换成历史拜访计划");
        //将拜访计划list转换为历史拜访计划list，然后插入历史拜访计划表
        List<VisitedPlanEntity> visitedPlanEntityList = new ArrayList<>();
        for (VisitingPlanEntity visitingPlanEntity : visitingPlanEntityList) {
            VisitedPlanEntity visitedPlanEntity = new VisitedPlanEntity();
            visitedPlanEntity.setUserId(visitingPlanEntity.getUserId());
            visitedPlanEntity.setBookDate(visitingPlanEntity.getBookDate());
            visitedPlanEntity.setPerson(visitingPlanEntity.getPerson());
            visitedPlanEntity.setOrgName(visitingPlanEntity.getOrgName());
            visitedPlanEntity.setDesc(visitingPlanEntity.getDesc());
            visitedPlanEntity.setProductionIds(visitingPlanEntity.getProductionIds());
            visitedPlanEntity.setTitle(visitingPlanEntity.getTitle());
            visitedPlanEntity.setTimestamp(visitingPlanEntity.getTimestamp());
            visitedPlanEntityList.add(visitedPlanEntity);
        }
        log.info("开始插入历史拜访计划");
        visitedPlanService.addList(visitedPlanEntityList);
        log.info("插入历史拜访计划完成");
        //插入成功后，删除拜访表中数据
        visitingPlanService.deleteList(visitingPlanEntityList);
        log.info("删除" + new Date(System.currentTimeMillis()) + "之前拜访计划");
    }
}
